package com.taotao.search.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.common.pojo.SearchResult;
import com.taotao.search.service.SearchService;

@Controller
public class SearchController {
	@Autowired
	private SearchService searchService;
	
	@Value("${ITEM_ROWS}")
	private Integer rows;
	
	@RequestMapping("/search")
	public String search(@RequestParam("q") String queryString,@RequestParam(defaultValue = "1") Integer page,Model model) throws Exception{
		//解决乱码
		queryString = new String(queryString.getBytes("ISO-8859-1"), "UTF-8");
		SearchResult searchResult = searchService.search(queryString, page, rows);
		model.addAttribute("query",queryString);
		model.addAttribute("totalPages",searchResult.getPageCount());
		model.addAttribute("itemList", searchResult.getItemList());
		model.addAttribute("page", page);
		return "search";
	}
}
